/*
 * @Date: 2023-08-12 17:14:56
 * @LastEditors: Wangjun
 * @LastEditTime: 2023-10-10 19:49:50
 * @FilePath: \drawing\src\lib\lib.ts
 * @Description: 
 */

import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'

function easyapi(url: string, method: string, body: any, cb: Function, dlgType?: string) {
    if (dlgType == null) {
        dlgType = "msg";
    }
    axios({ url: url, method: method, data: body }).then((rsp) => {
        if (rsp.data.code == null) {
            if (cb) {
                try {
                    cb(rsp);
                } catch (err) {
                    console.log(err)
                }
            }
            return;
        }

        if (rsp.data.code == 200) {
            if (cb) {
                try {
                    cb(rsp);
                } catch (err) {
                    console.log(err)
                }
            }
        } else {
            if (rsp.data) {
                ElMessage({
                    type: 'info',
                    message: rsp.data,
                })
            } else {
                ElMessage({
                    type: 'info',
                    message: "数据异常",
                })
            }
        }
    }).catch(function (error) {
        if (error.response) {
            // 请求成功发出且服务器也响应了状态码，但状态代码超出了 2xx 的范围
            switch (dlgType) {
                case "msg":
                    if (error.response.data) {
                        ElMessage({
                            type: 'info',
                            message: error.response.data,
                        })
                    } else {
                        ElMessage("服务通信异常")
                    }
                    break
                case "box":
                    if (error.response.data) {
                        ElMessageBox.alert(error.response.data, '错误', { confirmButtonText: '确定' })
                    } else {
                        ElMessageBox.alert("服务通信异常", '错误', { confirmButtonText: '确定' })
                    }
                    break;
            }
            console.log(error.response.data, error.response.status);
        } else if (error.request) {
            // 请求已经成功发起，但没有收到响应
            // `error.request` 在浏览器中是 XMLHttpRequest 的实例，
            // 而在node.js中是 http.ClientRequest 的实例
            console.log("服务器异常", error.request);
        } else {
            // 发送请求时出了点问题
            console.log('网络异常', error.message);
        }
        console.log(error.config);
    });
}


function getParams() {
    let url = location.href; //获取url中"?"符后的字串
    let params = {};
    let idx = url.indexOf("?")
    if (idx != -1) {
        let str = url.substring(idx + 1);
        let strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            params[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
        }
    }
    return params;
}

//极致精简id
let gid: number = 0;
let dict: any = { 0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 10: 'A', 11: 'B', 12: 'C', 13: 'D', 14: 'E', 15: 'F', 16: 'G', 17: 'H', 18: 'I', 19: 'J', 20: 'K', 21: 'L', 22: 'M', 23: 'N', 24: 'O', 25: 'P', 26: 'Q', 27: 'R', 28: 'S', 29: 'T', 30: 'U', 31: 'V', 32: 'W', 33: 'X', 34: 'Y', 35: 'Z', 36: 'a', 37: 'b', 38: 'c', 39: 'd', 40: 'e', 41: 'f', 42: 'g', 43: 'h', 44: 'i', 45: 'j', 46: 'k', 47: 'l', 48: 'm', 49: 'n', 50: 'o', 51: 'p', 52: 'q', 53: 'r', 54: 's', 55: 't', 56: 'u', 57: 'v', 58: 'w', 59: 'x', 60: 'y', 61: 'z', 62: '.', 63: '_' }
function TID() {
    gid++;
    if (gid >= 1000) {
        gid = 1
    }
    let text:string[] = [];
    //保证10年内没有重复的id
    let id = ((Math.floor(new Date().getTime() / 10000) % (86400 * 365 * 10)) * 1000 + gid);
    for (;id>0;id=id >>> 6){
        let idx = id&0x3f;
        text.push(dict[idx]);
    }
    text.reverse()    
    let sid:string = text.join('');
    //console.log(sid)
    return sid;
}

export {
    easyapi,
    getParams,
    TID
}